package com.shiwaixiangcun.customer.presenter.impl;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shiwaixiangcun.customer.Common;
import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.GlobalConfig;
import com.shiwaixiangcun.customer.entity.HouseSelectListBean;
import com.shiwaixiangcun.customer.entity.InformationBean;
import com.shiwaixiangcun.customer.entity.LoginResultBean;
import com.shiwaixiangcun.customer.entity.ResponseEntity;
import com.shiwaixiangcun.customer.network.HttpCallBack;
import com.shiwaixiangcun.customer.network.HttpRequest;
import com.shiwaixiangcun.customer.presenter.IOnHouseToSellerPresenter;
import com.shiwaixiangcun.customer.ui.IHouseToSellerView;
import com.shiwaixiangcun.customer.ui.dialog.DialogLoading;
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr;
import com.shiwaixiangcun.customer.utils.JsonUtil;
import com.shiwaixiangcun.customer.utils.LoginOutUtil;
import com.shiwaixiangcun.customer.utils.RefreshTokenUtil;
import com.shiwaixiangcun.customer.utils.SharePreference;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author Administrator
 * @date 2017/5/25
 */

public class HousetoSellerImpl implements IOnHouseToSellerPresenter {
    private IHouseToSellerView iHouseToSellerView;
    private String strHouseid;
    private String strContent;
    private DialogLoading mDialogLoading;

    public HousetoSellerImpl(IHouseToSellerView iHouseToSellerView, String strHouseid, String strContent) {
        this.iHouseToSellerView = iHouseToSellerView;
        this.strHouseid = strHouseid;
        this.strContent = strContent;
    }

    @Override
    public void setBgaAdpaterAndClick(Context context) {
        mDialogLoading = new DialogLoading(context, "正在提交");
        mDialogLoading.show();
        sendToRentHttp(context);
    }

    @Override
    public void setHouseList(Context context) {
        sendHouseListHttp(context);
    }

    @Override
    public void setInformation(Context context) {
        sendInformationHttp(context);
    }


    //出租房
    private void sendToRentHttp(final Context context) {

        final String refreshToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.Refresh_token, "");
        HashMap<String, Object> hashMap = new HashMap<>(3);
        hashMap.put("access_token", AppSharePreferenceMgr.get(context, GlobalConfig.TOKEN, ""));
        hashMap.put("content", strContent);
        hashMap.put("houseId", strHouseid);

        HttpRequest.post(GlobalApi.INSTANCE.getToSeller(), hashMap, new HttpCallBack() {
            @Override
            public void onSuccess(String responseJson) {
                Type type = new TypeToken<ResponseEntity>() {
                }.getType();
                ResponseEntity responseEntity = JsonUtil.fromJson(responseJson, ResponseEntity.class);
                if (responseEntity.getResponseCode() == 1001) {
                    iHouseToSellerView.setBgaAdpaterAndClickResult(responseEntity);
                    mDialogLoading.close();
                } else if (responseEntity.getResponseCode() == 1018) {
                    RefreshTokenUtil.refreshToken(context, refreshToken);
                } else if (responseEntity.getResponseCode() == 1019) {
                    LoginOutUtil.sendLoginOutUtil(context);
                }else if (responseEntity.getResponseCode() == 1002){
                    Toast.makeText(context,responseEntity.getMessage(),Toast.LENGTH_LONG).show();
                    mDialogLoading.close();
                }

            }

            @Override
            public void onFailed(Exception e) {
                Log.i("oooooo---onFailed---", e.toString());
                Toast.makeText(context,"网络异常，请稍后再试...",Toast.LENGTH_LONG).show();
                mDialogLoading.close();
            }
        });
    }


    //房列表
    private void sendHouseListHttp(final Context context) {
        String login_detail = SharePreference.getStringSpParams(context, Common.IS_SAVE_LOGIN, Common.SISAVELOGIN);
        Log.i("eeeeeettt", login_detail);
        Type type = new TypeToken<ResponseEntity<LoginResultBean>>() {
        }.getType();
        ResponseEntity<LoginResultBean> responseEntity = JsonUtil.fromJson(login_detail, type);
        final String refresh_token = responseEntity.getData().getRefreshToken();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("access_token", responseEntity.getData().getAccessToken());
        hashMap.put("fields", "id,numberDesc");


        HttpRequest.get(GlobalApi.INSTANCE.getAssociatedHouses(), hashMap, new HttpCallBack() {
            @Override
            public void onSuccess(String responseJson) {
                Type type = new TypeToken<ResponseEntity<List<HouseSelectListBean>>>() {
                }.getType();
                ResponseEntity<List<HouseSelectListBean>> responseEntity = JsonUtil.fromJson(responseJson, type);
                if (null != responseEntity.getData()) {
                    if (responseEntity.getResponseCode() == 1001) {
                        List<HouseSelectListBean> data = responseEntity.getData();
                        iHouseToSellerView.setHouseListResult(data);
                    } else if (responseEntity.getResponseCode() == 1018) {
                        RefreshTokenUtil.refreshToken(context, refresh_token);
                    } else if (responseEntity.getResponseCode() == 1019) {
                        LoginOutUtil.sendLoginOutUtil(context);
                    }
                }


            }

            @Override
            public void onFailed(Exception e) {
                Log.i("oooooo---onFailed---", e.toString());
            }
        });
    }

    //个人信息
    private void sendInformationHttp(final Context context) {
        String login_detail = SharePreference.getStringSpParams(context, Common.IS_SAVE_LOGIN, Common.SISAVELOGIN);
        Log.i("eeeeeettt", login_detail);
        Type type = new TypeToken<ResponseEntity<LoginResultBean>>() {
        }.getType();
        final ResponseEntity<LoginResultBean> responseEntity = JsonUtil.fromJson(login_detail, type);
        final String refresh_token = responseEntity.getData().getRefreshToken();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("access_token", responseEntity.getData().getAccessToken());

        HttpRequest.get(GlobalApi.INSTANCE.getInformation(), hashMap, new HttpCallBack() {
            @Override
            public void onSuccess(String responseJson) {
                InformationBean user = new Gson().fromJson(responseJson, InformationBean.class);
                if (user.getResponseCode() == 1001) {
                    iHouseToSellerView.setInformationResult(user);
                } else if (user.getResponseCode() == 1018) {
                    RefreshTokenUtil.refreshToken(context, refresh_token);
                } else if (user.getResponseCode() == 1019) {
                    LoginOutUtil.sendLoginOutUtil(context);
                }
            }

            @Override
            public void onFailed(Exception e) {
                Log.i("oooooo---onFailed---", e.toString());
            }
        });
    }


}
